/***************************************************************
 * Name:      stringsMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    group3 ()
 * Created:   2012-04-14
 * Copyright: group3 ()
 * License:
 **************************************************************/

#include "stringsMain.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(stringsFrame)
#include <wx/string.h>
#include <wx/intl.h>
//*)

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(stringsFrame)
const long stringsFrame::ID_TEXTCTRL1 = wxNewId();
const long stringsFrame::idMenuOpen = wxNewId();
const long stringsFrame::idMenuClear = wxNewId();
const long stringsFrame::idMenuQuit = wxNewId();
const long stringsFrame::idMenuAbout = wxNewId();
//*)

BEGIN_EVENT_TABLE(stringsFrame,wxFrame)
    //(*EventTable(stringsFrame)
    //*)
END_EVENT_TABLE()

stringsFrame::stringsFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(stringsFrame)
    wxMenuItem* MenuItem2;
    wxMenuItem* MenuItem1;
    wxMenu* Menu1;
    wxMenuItem* MenuItem3;
    wxMenuBar* MenuBar1;
    wxMenu* Menu2;

    Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));
    SetClientSize(wxSize(435,247));
    TextCtrl1 = new wxTextCtrl(this, ID_TEXTCTRL1, wxEmptyString, wxPoint(112,184), wxDefaultSize, wxTE_MULTILINE|wxTE_RICH, wxDefaultValidator, _T("ID_TEXTCTRL1"));
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem3 = new wxMenuItem(Menu1, idMenuOpen, _("Open\tCtrl+O"), _("Open new txt file"), wxITEM_NORMAL);
    Menu1->Append(MenuItem3);
    MenuItem4 = new wxMenuItem(Menu1, idMenuClear, _("Clear\tCtrl+N"), _("Clear statistics"), wxITEM_NORMAL);
    Menu1->Append(MenuItem4);
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    FileDialog1 = new wxFileDialog(this, _("Select file"), wxEmptyString, wxEmptyString, _("Text files (*.txt)|*.txt"), wxFD_DEFAULT_STYLE, wxDefaultPosition, wxDefaultSize, _T("wxFileDialog"));

    Connect(idMenuOpen,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&stringsFrame::OnOpen);
    Connect(idMenuClear,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&stringsFrame::OnClear);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&stringsFrame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&stringsFrame::OnAbout);
    //*)
    statisticsTotal = 0;
    others = 0;
    SetTitle(_("No files added yet"));
}

stringsFrame::~stringsFrame()
{
    //(*Destroy(stringsFrame)
    //*)
    statistics.clear();
}

void stringsFrame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void stringsFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void stringsFrame::OnOpen(wxCommandEvent& event)
{
    if (FileDialog1->ShowModal() == wxID_OK) // if the user click "Open" instead of "cancel"
	{
	    wxString CurrentDocPath = FileDialog1->GetPath();
        wxTextFile CurrentTextFile;

        CurrentTextFile.Open(CurrentDocPath,wxConvISO8859_1);
        SetTitle(_("Last file added - ") + CurrentDocPath);

        for ( wxString CurrentLine = CurrentTextFile.GetFirstLine().Lower(); !CurrentTextFile.Eof(); CurrentLine = CurrentTextFile.GetNextLine().Lower() )
        {
            for (unsigned i=0; i<CurrentLine.Len(); i++)
            {
                wxString CurWxChar = CurrentLine[i];
                OnClean(CurWxChar);

                if (alphabet.Contains(CurWxChar)){statistics[CurWxChar]++;}
                else {others++;}
                statisticsTotal++;
            }
        }
        CurrentTextFile.Close();
        CurrentDocPath.Clear();
        TextCtrl1->Clear();

        OnDump();
    }
}


void stringsFrame::OnClear(wxCommandEvent& event)
{
    statisticsTotal = 0;
    others = 0;
    SetTitle(_("No files added yet"));
    statistics.clear();
    TextCtrl1->Clear();
}

void stringsFrame::OnDump()
{
	for (statisticsIt=statistics.begin(); statisticsIt!=statistics.end(); statisticsIt++)
	{
        TextCtrl1->WriteText(WriteLine((*statisticsIt).first, (*statisticsIt).second));
	}
    TextCtrl1->WriteText(WriteLine(_("others"),others));
}

void stringsFrame::OnClean(wxString &a)
{
    if (charA.Contains(a)) {a=wxT("a");}
    else if (charE.Contains(a)) {a=wxT("e");}
    else if (charI.Contains(a)) {a=wxT("i");}
    else if (charO.Contains(a)) {a=wxT("o");}
    else if (charU.Contains(a)) {a=wxT("u");}
    else if (charC.Contains(a)) {a=wxT("c");}
    else if (charN.Contains(a)) {a=wxT("n");}
}

wxString stringsFrame::WriteLine(wxString a, int b)
{
    wxString LineCount = wxString::Format(wxT("%i"), b);
    wxString LineStat = wxString::Format(wxT("%f"),(b*100/(float)statisticsTotal));

    return (a + _T(" : ") + LineCount + _T(" : ") + LineStat + _T("%") + _T("\n"));
}
